package com.nowcoder.community.service;
import com.nowcoder.community.dao.MessageMapper;
import com.nowcoder.community.entity.Message;
import com.nowcoder.community.util.SensitiveFilter;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
@Service
public class MessageService {
    @Resource
    private MessageMapper messageMapper;
    @Resource
    private SensitiveFilter sensitiveFilter;
    public List<Message>findConversations(int userId,int offset,int limit){
        return messageMapper.selectConversations(userId, offset, limit);
    }
    //查询当前用户的会话列表
    public int findConversationCount(int userId){
        return messageMapper.selectConversationCount(userId);
    }
    //查询当前用户会话数量
    public List<Message> findLetters(String conversationId, int offset, int limit) {
        return messageMapper.selectLetters(conversationId, offset, limit);
    }
    //查询该会话的私信列表
    public int findLetterCount(String conversationId){return messageMapper.selectLetterCount(conversationId);}
    //查询该会话的私信数量
    public int findLetterUnreadCount(int userId, String conversationId) {
        return messageMapper.selectLetterUnreadCount(userId, conversationId);
    }
    //查询该用户的该会话未读的私信数量
    public int addMessage(Message message){
        //给信息进行html转义和过滤敏感词之后存入
        message.setContent(HtmlUtils.htmlEscape(
                sensitiveFilter.filter(
                        message.getContent()
                )
        ));
        return messageMapper.insertMessage(message);
    }
    //查询该用户的该会话未读的私信数量
    public int readMessage(List<Integer>ids){
        return messageMapper.updateStatus(ids,1);
    }
    //将列表id的信息都标记为已读
    public int deleteMessage(int id) {
        return messageMapper.updateStatus(List.of(id),2);
    }
    //删除对应id的信息
    public Message findLatestNotice(int userId,String topic){
        return messageMapper.selectLatestNotice(userId,topic);
    }
    //查找最新的通知
    public int findNoticeCount(int userId,String topic){
        return messageMapper.selectNoticeCount(userId, topic);
    }
    //查找总的通知数量
    public int findNoticeUnreadCount(int userId,String topic){
        return messageMapper.selectNoticeUnreadCount(userId,topic);
    }
    //查找未读通知数量
    public List<Message>findNotices(int userId,String topic,int offset,int limit ){
        return messageMapper.selectNotices(userId, topic, offset, limit);
    }
    //查找通知列表
}
